package com.admin.scroller.photoorvideo.adapter;

import android.app.ActivityOptions;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.admin.scroller.photoorvideo.PerivewActivity;
import com.admin.scroller.photoorvideo.PhotoChoseActivity;
import com.admin.scroller.photoorvideo.R;
import com.admin.scroller.photoorvideo.VideoPreviewActivity;
import com.admin.scroller.photoorvideo.beans.LocalPhoto;
import com.admin.scroller.photoorvideo.view.ZoomImageView;
import com.bumptech.glide.Glide;

import java.io.File;
import java.util.List;

public class PreviewAdapter extends PagerAdapter {

    private List<LocalPhoto> localPhotos;
    private Context context;
    private SparseArray<View> viewSparseArray=new SparseArray<>();
    private View view;
    private ZoomImageView zoomImageView;
    private ImageView imageView;

    public PreviewAdapter(Context context){
        this.context=context;
    }

    public void initAdapterData(List<LocalPhoto> localPhotos){
        this.localPhotos=localPhotos;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return localPhotos==null?0:localPhotos.size();
    }

    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
        return view==object;
    }

    @NonNull
    @Override
    public Object instantiateItem(@NonNull final ViewGroup container, final int position) {
        view= LayoutInflater.from(context).inflate(R.layout.fragment_perview,null);
        zoomImageView=view.findViewById(R.id.zim_image);
        imageView=view.findViewById(R.id.iv_play_video);
        if(localPhotos.get(position).isVideo()){
            imageView.setVisibility(View.VISIBLE);
            Glide.with(context).load(new File(localPhotos.get(position).getVideoUrl())).into(zoomImageView);
            imageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent=new Intent(context, VideoPreviewActivity.class);
                    intent.putExtra("url",localPhotos.get(position).getVideoUrl());
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                        context.startActivity(intent, ActivityOptions.makeSceneTransitionAnimation((PerivewActivity)context, imageView, context.getString(R.string.activity_show)).toBundle());
                    }else {
                        context.startActivity(intent);
                    }
                }
            });
        }else {
            imageView.setVisibility(View.GONE);
            Glide.with(context).load(new File(localPhotos.get(position).getPath())).into(zoomImageView);
        }
        container.addView(view,0);
        return view;
    }

    @Override
    public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
        container.removeView((View)object);
    }

    @Override
    public int getItemPosition(@NonNull Object object) {
        return POSITION_NONE;
    }

}
